package com.hhp.hhptt.controller;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhp.hhptt.common.Result;
import com.hhp.hhptt.entity.Wxuser;
import com.hhp.hhptt.service.IWxuserService;
import com.hhp.hhptt.service.impl.WxuserServiceImpl;
import com.hhp.hhptt.utils.WeiXinUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/weChat")
public class WeChatController {

    @Resource
    private IWxuserService wxUserService;
//    @Autowired
//    WxuserServiceImpl wxUserService;
    @ResponseBody
    @GetMapping("/verifyToken")
    @ApiOperation("微信Token验证")
    public String verifyToken(HttpServletRequest request) {
        return request.getParameter("echostr");
    }

    @GetMapping("/getCode")
    @ApiOperation("获取微信code")
    public String weiXinLogin(@RequestParam(defaultValue = "123") String state) {
        return WeiXinUtil.getCode(state);
    }

    @ResponseBody
    @GetMapping("/getAccessToken")
    @ApiOperation("获取token")
    public String getAccessToken(@RequestParam String code) {
        Map result = WeiXinUtil.getAccessToken(code);
        String accessToken = result.get("access_token").toString();
        String refreshToken = result.get("refresh_token").toString();
        String openid = result.get("openid").toString();
        Wxuser wxUser = new Wxuser();
        wxUser.setAccessToken(accessToken);
        wxUser.setRefreshToken(refreshToken);
        wxUser.setCreateTime( new Date());
        wxUser.setUserId(1L);
        wxUser.setOpenid(openid);
        wxUserService.save(wxUser);
        return "<h1>看样子成功了,没你事了退下吧</h1>";
    }

    @ResponseBody
    @GetMapping("/refreshToken")
    @ApiOperation("刷新token")
    public Result refreshToken() {
        Long userId = 1L;
        Wxuser wxUser = wxUserService.getOne(new LambdaUpdateWrapper<Wxuser>().eq(Wxuser::getUserId, userId));
        Map result = WeiXinUtil.refreshToken(wxUser.getRefreshToken());
        String accessToken = result.get("access_token").toString();
        String refreshToken = result.get("refresh_token").toString();
        Wxuser wxUserUpdate = new Wxuser();
        wxUserUpdate.setId(wxUser.getId());
        wxUserUpdate.setAccessToken(accessToken);
        wxUserUpdate.setRefreshToken(refreshToken);
        wxUserUpdate.setUpdateTime(new Date());
        wxUserService.updateById(wxUserUpdate);
        return Result.success(accessToken);
    }

    @ResponseBody
    @GetMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public Result getUserInfo() {
        Long userId = 1L;
        Wxuser wxUser = wxUserService.getOne(new LambdaUpdateWrapper<Wxuser>().eq(Wxuser::getUserId, userId));
        String accessToken = wxUser.getAccessToken();
        String openid = wxUser.getOpenid();
        String userInfoJsom = WeiXinUtil.getUserInfo(accessToken, openid);
        Wxuser userInfo = JSONObject.parseObject(userInfoJsom, Wxuser.class);
        userInfo.setId(wxUser.getId());
        userInfo.setUpdateTime(new Date());
        wxUserService.updateById(userInfo);
        return Result.success(userInfo);
    }

    @PostMapping("/save")
    public Boolean save(@RequestBody Wxuser wxuser) {
        return wxUserService.saveOrUpdate(wxuser);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return wxUserService.removeById(id);
    }
    @PostMapping("/deleteBatch")
    public Boolean deleteBatch(@RequestBody List<Integer> ids) {
        return wxUserService.removeByIds(ids);
    }

    @GetMapping("/getAll")
    public List<Wxuser> findAll() {
        return wxUserService.list();
    }
    @GetMapping("/{id}")
    public Wxuser findOne(@PathVariable Integer id) {
        return wxUserService.getById(id);
    }

    @GetMapping("/page")
    public Page<Wxuser> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize) {

        QueryWrapper<Wxuser> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return wxUserService.page(new Page<>(pageNum, pageSize),queryWrapper);
    }

}
